// bookgo/public_login/public_login.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        aid:'',
        alertShow:false,
        reader_id:'',
        password:'',
        reader_openid:'',
        reader_id:'',
        rules: [],
        name:'',
        rid:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) { 
        // if (!options.aid){
        //     wx.redirectTo({
        //         url: '/bookgo/index/index',
        //     })
        // }
        this.setData({
            // aid:options.aid 
            aid:3010
        });
    },
    showAlert:function(){
        this.setData({
        alertShow: !this.data.alertShow
        });
    },
    showAlert_rules: function() {
        const that = this;
        if (that.data.rules.length == 0) {
            wx.request({
                url: 'https://2016.bookgo.com.cn/wxapp/index/get_public_rule',
                data: {},
                header: {
                    'content-type': 'application/json'
                },
                success: function (res) {
                    console.log(res.data);
                    var data = res.data;
                    var items = [];
                    for (var i = 0; i < data.length; i++) {
                        var item = {
                            text: data[i].text
                        };
                    items.push(item);
                    }
                    console.log(items)
                    that.setData({
                        rules: items
                    });
                },
            });
        }
        // 弹出框显示
        this.setData({
            alertShow: true
        });
    },


    formSubmit: function (e) {
        console.log(e);
        var that = this;
        // wx.setStorageSync('reader_id',that.data.reader_id);
        wx.showLoading({
        title: '请求中',
        })
        wx.request({
        url: 'https://2016.bookgo.com.cn/wxapp/api/public_login',
        data:{
            stuid:that.data.reader_id,
            password:that.data.password,
            aid:that.data.aid
        },
        method:'POST',
            success:function(res){
                console.log(res);
                wx.hideLoading();
                if(res.data.code == 101){
                    that.setData({
                        reader_id:res.data.data.reader_id,
                        reader_openid:res.data.data.openid,
                        name: res.data.data.name,
                        rid:res.data.data.id

                    });
                    console.log('-------=======----formSubmit----=======');
                    console.log(res.data.data);
                    wx.setStorageSync('reader_user', res.data.data);
                    that.public_bind();
                }else{
                    wx.showModal({
                        title: '提示',
                        content: res.data.data,
                        showCancel:false,
                        success:function(res){
                            console.log(res); 
                        }
                    })
                }
            }
        })
    },

        public_bind(){
            const that = this;
            var openid = wx.getStorageSync('openid');

            console.log('-------------------缓存的openid !!!----------------');
            console.log(openid);

            console.log(that.data.password);
            wx.request({
            url: 'https://2016.bookgo.com.cn/wxapp/api/public_bind',
            data:{
                openid: openid,
                aid:that.data.aid,
                reader_id:that.data.reader_id,
                password:that.data.password,
                reader_openid:that.data.reader_openid
            },
                success:function(res){
                    console.log('=======================');
                    console.log(res);
                    if(res.data.code == 101){
                        console.log('!!!!!!!!!!!!!!!!!!!!!!');
                        console.log(res.data.msg);
                        wx.setStorageSync(that.data.aid + 'user', res.data.msg);
                            wx.redirectTo({
                                url: '/bookgo/borrow/borrow?aid=' + that.data.aid,
                            })
                    }else{
                        wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        showCancel: false,
                            success: function (res) {
                                console.log(res);
                            }
                        })
                    }
                }
            });    
        },

        bindStuidInput:function(e){
            this.setData({
            reader_id:e.detail.value
            });
        },
        bindPasswordInput:function(e){
            this.setData({
            password:e.detail.value
            });
        },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
    
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
    
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
    
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
    
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
    
    },


})